/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ids.util;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Chartha Senarathne
 */
public class FileUtil {

    public static ArrayList<String> readFile(String path) {
        File file = new File(path);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;
        ArrayList<String> lines = new ArrayList<String>();
        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            dis = new DataInputStream(bis);

            try {
                // dis.available() returns 0 if the file does not have more lines.
                while (dis.available() != 0) {
                    lines.add(dis.readLine());
                }
            } catch (IOException ex) {
                Logger.getLogger(FileUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
            fis.close();
            bis.close();
            dis.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return lines;
    }   
    
    public static ArrayList<String> searchWordWithinLines(ArrayList<String> lines, String keyWord, ArrayList<String> searchedResult)
    {
        for (String line : lines) {
            if(line.indexOf(keyWord) != -1)
            {
                searchedResult.add(line);
            }
        }
        return searchedResult;
    }
}
